package com.cci.kangdao.service.impl;

import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.dao.OcrOrderVehicleLicenseRecordMapper;
import com.cci.kangdao.dao.WorkOrderTDao;
import com.cci.kangdao.dao.model.OcrOrderVehicleLicenseRecord;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.dao.model.WorkOrderT;
import com.cci.kangdao.dto.BaseResponse;
import com.cci.kangdao.dto.request.OcrOrderVehicleLicenseRecordDtoRequest;
import com.cci.kangdao.dto.request.OcrOrderVehicleLicenseRecordRequest;
import com.cci.kangdao.service.OcrOrderVehicleLicenseRecordTService;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: joe
 * @company: CTY
 * @since: 2023/11/8 16:05
 * @version: V1.0
 **/
@Service
public class OcrOrderVehicleLicenseRecordTServiceImpl implements OcrOrderVehicleLicenseRecordTService {
    @Resource
    private OcrOrderVehicleLicenseRecordMapper ocrOrderVehicleLicenseRecordTMapper;
    @Resource
    private WorkOrderTDao workOrderTDao;
    /**
     * 【智能索赔】【DBU工单】【BE】智能索赔识别字段埋点
     * @param ocrOrderVehicleLicenseRecordRequest
     * @return
     */
    @Override
    @Transactional
    public BaseResponse insert(OcrOrderVehicleLicenseRecordRequest ocrOrderVehicleLicenseRecordRequest) {
        //查询工单信息
        if (null==ocrOrderVehicleLicenseRecordRequest.getOrderId()) {
            return BaseResponse.error("请输入orderid");
        }
        if (CollectionUtils.isNotEmpty(ocrOrderVehicleLicenseRecordRequest.getList())) {
            UserT userT = UserThreadLocal.get();
            WorkOrderT workOrderT = workOrderTDao.getWorkOrderTByOrderId(ocrOrderVehicleLicenseRecordRequest.getOrderId());
            if (null==workOrderT) {
                return BaseResponse.error("请输入正确的orderid");
            }
            OcrOrderVehicleLicenseRecord select = ocrOrderVehicleLicenseRecordTMapper.selectFirst(workOrderT.getId());
            if (null==select) {
                for (OcrOrderVehicleLicenseRecordDtoRequest ocrOrderVehicleLicense : ocrOrderVehicleLicenseRecordRequest.getList()) {
                    OcrOrderVehicleLicenseRecord ocrOrderVehicleLicenseRecord = new OcrOrderVehicleLicenseRecord();
                    ocrOrderVehicleLicenseRecord.setPicName(ocrOrderVehicleLicense.getPicName());
                    ocrOrderVehicleLicenseRecord.setOrderId(workOrderT.getId());
                    ocrOrderVehicleLicenseRecord.setOrderNo(workOrderT.getOrderNo());
                    ocrOrderVehicleLicenseRecord.setChannel(workOrderT.getOrderChannel());
                    ocrOrderVehicleLicenseRecord.setOrderNoCrm(workOrderT.getOrderNoCrm());
                    ocrOrderVehicleLicenseRecord.setESN(workOrderT.getESN());
                    ocrOrderVehicleLicenseRecord.setWarrantyStartDate(workOrderT.getWarrantyStartDate());
                    ocrOrderVehicleLicenseRecord.setPhone(userT.getPhone());
                    ocrOrderVehicleLicenseRecord.setVIN(workOrderT.getVIN());
                    ocrOrderVehicleLicenseRecord.setChassisNumber(workOrderT.getChassisNumber());
                    ocrOrderVehicleLicenseRecord.setOEMName(workOrderT.getoEMName());
                    ocrOrderVehicleLicenseRecord.setUserId(userT.getId());
                    ocrOrderVehicleLicenseRecord.setRoleName("服务技师");
                    ocrOrderVehicleLicenseRecord.setRoleNo("R0033");
                    ocrOrderVehicleLicenseRecord.setAction(ocrOrderVehicleLicense.getActtion());
                    ocrOrderVehicleLicenseRecord.setActionOldValue(this.getValue(ocrOrderVehicleLicense.getActtion(),workOrderT));
                    ocrOrderVehicleLicenseRecord.setOcrContent(ocrOrderVehicleLicense.getOcr());
                    ocrOrderVehicleLicenseRecord.setIsModify(StringUtils.isNotBlank(ocrOrderVehicleLicense.getEdit())?1:0);
                    ocrOrderVehicleLicenseRecord.setModifyContent(ocrOrderVehicleLicense.getEdit());
                    ocrOrderVehicleLicenseRecord.setIsReplace(StringUtils.isNotBlank(ocrOrderVehicleLicense.getReplace())?1:0);
                    ocrOrderVehicleLicenseRecord.setIsFail(StringUtils.isNotBlank(ocrOrderVehicleLicense.getOcr())?1:0);
                    ocrOrderVehicleLicenseRecord.setModifyWorkCompare(this.getEdit(ocrOrderVehicleLicense.getEdit(),ocrOrderVehicleLicense.getActtion(),workOrderT));
                    ocrOrderVehicleLicenseRecord.setOcrModifyCompare(this.getOrcCompareWithEdit(ocrOrderVehicleLicense.getEdit(),ocrOrderVehicleLicense.getOcr()));
                    ocrOrderVehicleLicenseRecord.setFlag(0);
                    ocrOrderVehicleLicenseRecord.setCreator(userT.getId());
                    ocrOrderVehicleLicenseRecord.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                    ocrOrderVehicleLicenseRecordTMapper.insertSelective(ocrOrderVehicleLicenseRecord);
                }
            }else {
                workOrderT.setoEMName(select.getOEMName());
                workOrderT.setWarrantyStartDate(select.getWarrantyStartDate());
                workOrderT.setVIN(select.getVIN());
                workOrderT.setChassisNumber(select.getChassisNumber());
                for (OcrOrderVehicleLicenseRecordDtoRequest ocrOrderVehicleLicense : ocrOrderVehicleLicenseRecordRequest.getList()) {
                    OcrOrderVehicleLicenseRecord ocrOrderVehicleLicenseRecord = new OcrOrderVehicleLicenseRecord();
                    ocrOrderVehicleLicenseRecord.setPicName(ocrOrderVehicleLicense.getPicName());
                    ocrOrderVehicleLicenseRecord.setOrderId(select.getOrderId());
                    ocrOrderVehicleLicenseRecord.setOrderNo(select.getOrderNo());
                    ocrOrderVehicleLicenseRecord.setChannel(select.getChannel());
                    ocrOrderVehicleLicenseRecord.setOrderNoCrm(select.getOrderNoCrm());
                    ocrOrderVehicleLicenseRecord.setESN(select.getESN());
                    ocrOrderVehicleLicenseRecord.setWarrantyStartDate(select.getWarrantyStartDate());
                    ocrOrderVehicleLicenseRecord.setPhone(userT.getPhone());
                    ocrOrderVehicleLicenseRecord.setVIN(select.getVIN());
                    ocrOrderVehicleLicenseRecord.setChassisNumber(select.getChassisNumber());
                    ocrOrderVehicleLicenseRecord.setOEMName(select.getOEMName());
                    ocrOrderVehicleLicenseRecord.setUserId(userT.getId());
                    ocrOrderVehicleLicenseRecord.setRoleName("服务技师");
                    ocrOrderVehicleLicenseRecord.setRoleNo("R0033");
                    ocrOrderVehicleLicenseRecord.setAction(ocrOrderVehicleLicense.getActtion());
                    ocrOrderVehicleLicenseRecord.setActionOldValue(this.getValue(ocrOrderVehicleLicense.getActtion(),workOrderT));
                    ocrOrderVehicleLicenseRecord.setOcrContent(ocrOrderVehicleLicense.getOcr());
                    ocrOrderVehicleLicenseRecord.setIsModify(StringUtils.isNotBlank(ocrOrderVehicleLicense.getEdit())?1:0);
                    ocrOrderVehicleLicenseRecord.setModifyContent(ocrOrderVehicleLicense.getEdit());
                    ocrOrderVehicleLicenseRecord.setIsReplace(StringUtils.isNotBlank(ocrOrderVehicleLicense.getReplace())?1:0);
                    ocrOrderVehicleLicenseRecord.setIsFail(StringUtils.isNotBlank(ocrOrderVehicleLicense.getOcr())?1:0);
                    ocrOrderVehicleLicenseRecord.setModifyWorkCompare(this.getEdit(ocrOrderVehicleLicense.getEdit(),ocrOrderVehicleLicense.getActtion(),workOrderT));
                    ocrOrderVehicleLicenseRecord.setOcrModifyCompare(this.getOrcCompareWithEdit(ocrOrderVehicleLicense.getEdit(),ocrOrderVehicleLicense.getOcr()));
                    ocrOrderVehicleLicenseRecord.setFlag(0);
                    ocrOrderVehicleLicenseRecord.setCreator(userT.getId());
                    ocrOrderVehicleLicenseRecord.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                    OcrOrderVehicleLicenseRecord updateSelect = ocrOrderVehicleLicenseRecordTMapper.selectList(workOrderT.getId(),ocrOrderVehicleLicense.getActtion());
                    if (updateSelect==null) {
                        ocrOrderVehicleLicenseRecordTMapper.insertSelective(ocrOrderVehicleLicenseRecord);
                    }else {
                        ocrOrderVehicleLicenseRecord.setId(updateSelect.getId());
                        ocrOrderVehicleLicenseRecordTMapper.updateByPrimaryKeySelective(ocrOrderVehicleLicenseRecord);
                    }
                }
            }
        }
        return BaseResponse.success();
    }

    public String getValue(String action, WorkOrderT workOrderT){
        String str = "";
        Class<? extends WorkOrderT> aClass = workOrderT.getClass();
        try {
            Field declaredField = aClass.getDeclaredField(action);
            declaredField.setAccessible(true);
            str = (String)declaredField.get(workOrderT);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return str;
    }

    public String getOrcCompareWithEdit(String edit,String ocr){
        if (StringUtils.isNotBlank(ocr)&&StringUtils.isNotBlank(edit)) {
            if (ocr.equals(edit)) {
                return "一致";
            }else {
                return "不一致";
            }
        }else if (StringUtils.isNotBlank(edit)){
            return "技师未修改OCR数据";
        }else {
            return "未识别";
        }
    }


    public String getEdit(String edit,String action, WorkOrderT workOrderT){
        String str="";
        if (StringUtils.isBlank(edit)) {
            str = "技师未修改OCR数据";
            return str;
        }
        Class<? extends WorkOrderT> aClass = workOrderT.getClass();
        try {
            Field declaredField = aClass.getDeclaredField(action);
            declaredField.setAccessible(true);
            String value = (String)declaredField.get(workOrderT);
            if (StringUtils.isNotBlank(value)&&value.equals(edit)) {
                str = "一致";
            }else {
                str = "不一致";
            }
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return str;
    }
}
